package cn.edu.nju.software.grapheditor.cmd;

import java.awt.Graphics;
import java.awt.Point;

import cn.edu.nju.software.grapheditor.Drawing;
import cn.edu.nju.software.grapheditor.shape.Oval;

/**@author smy*/
//Completed - 10:58 P.M, May 21th
public class OvalCmd extends Command{
	private int beginx=0;
	private int beginy=0;
	private int endx;
	private int endy;
	private int currentwidth;
	private int currentheight;
	private Graphics g;
	private Oval tobepainted;
	private Oval previous;
	private boolean newone;
	public OvalCmd(Graphics g){
		this.g=g;
	}

	public void executePress(Point p, Drawing dwg) {
		this.beginx=p.x;
		this.beginy=p.y;
		newone=true;
	}

	public void executeDrag(Point p, Drawing dwg) {
		if(this.tobepainted!=null)previous=tobepainted;
		if(newone)previous=null;
		newone=false;
		this.endx=p.x;
		this.endy=p.y;
		this.currentwidth=Math.abs(endx-beginx);
		this.currentheight=Math.abs(endy-beginy);
		tobepainted=new Oval(dwg.getColor(),Math.min(this.beginx,this.endx),Math.min(this.beginy,this.endy),currentwidth,currentheight);
		dwg.addshape(tobepainted);
		dwg.delshape(previous);
		dwg.draw(g);
	}
}
